import 'dart:ui';
import 'package:flustars/flustars.dart';
import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';

import 'package:flutter_basic_widgets/image/image_load.dart';
import 'package:flutter_lang/flutter_lang.dart';
import 'package:flutter_lifecycle_base/base_state_widget.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_authentication/PIN/models/wallet_info_model.dart';
import 'package:wallet_authentication/utils/aes_util.dart';
import '../../module_config.dart';
import '../../image_r.dart';

class WalletStartPage extends BaseStatefulWidget {
  @override
  _WalletStartPageState createState() => _WalletStartPageState();
}

class _WalletStartPageState extends BaseWidgetState<WalletStartPage> {
  bool _isSkipCheckPin = false;
  @override
  void initState() {
    super.initState();

    /// 本地没有存助记词，则默认需创建钱包，否则跳转到pin码验证界面
    _isSkipCheckPin = AESUtil.decryptString(SpUtil.getString("mnemonic_list")).isNotEmpty;
   // _isSkipCheckPin = false;s
    if (mounted && _isSkipCheckPin) {
      _skipToCheckPinPage(context);
    }
  }

  void _skipToCheckPinPage(BuildContext context) {
    Future.delayed(Duration(milliseconds: 0), () {
      WXNavigator.pushNamedAndRemoveUntil("/checkPINPage", (route) => true,
          context: context,
          moduleName: "wallet_authentication",
          arguments: {
            'isHiddenBack': true,
            'onCallBack': (String value) {
              /// 跳主页逻辑
              print('---> wallet_home');
              Future.delayed(Duration(milliseconds: 110), () {
                WXNavigator.pushNamedAndRemoveUntil(
                    "/wallet_home", (route) => false,
                    context: context, moduleName: "wallet_home");
              });
            }
          });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (kReleaseMode) {
      print('release');
    } else {
      print('debug');
    }

    /// 本地没有存助记词，则默认需创建钱包，默认闪屏界面为主题色控件
    if (_isSkipCheckPin) {
      return Container(
        color: Theme.of(context).primaryColor,
      );
    }

    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
            color: Color(0xFF0046D2),
            gradient: RadialGradient(radius: 1.6, colors: [
              Color(0xFF0046D2),
              Color(0xFF00A0FF),
              Color(0xFFFFFFFF)
            ])),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(top: 120),
              child: ImageLoad(
                ImageR.imagesStartLogoIcon,
                width: 150,
                height: 160,
                fit: BoxFit.fill,
                package: ModuleConfig.moduleName,
              ),
            ),
            Spacer(),
//            FIXME：此处两个container可以写成一个方法
            Container(
                height: 55,
                width: 200,
                margin: EdgeInsets.only(bottom: 30),
                decoration: BoxDecoration(
                  color: Color(0xFFa3caeb),
                  border: Border.all(width: 1.0, color: Colors.white),
                  borderRadius: BorderRadius.all(Radius.circular(6.0)),
                ),
                child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    child: Material(
                      color: Colors.transparent,
                      child: Ink(
                        child: InkWell(
                          borderRadius: BorderRadius.all(Radius.circular(6.0)),
                          child: Container(
                            child: Text(
                              "${WXLang.$t("authentication_create_wallet", package: ModuleConfig.moduleName)}",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 16),
                            ),
                            alignment: Alignment.center,
                          ),
                          onTap: () {
                            WXNavigator.pushNamed(
                                context: context,
                                routeName: '/walletCreate',
                                // arguments: WalletSource.create
                                arguments: 0
                                //  {
                                //   "isUpDate": false,
                                //   "walletSource": WalletSource.create
                                // }
                                );
                          },
                        ),
                      ),
                    )
                )
            ),
            Container(
                alignment: Alignment.center,
                height: 55,
                width: 200,
                margin: EdgeInsets.only(bottom: 100),
                decoration: BoxDecoration(
                  border: Border.all(width: 1.0, color: Color(0xFFb4d1e1)),
                  borderRadius: BorderRadius.all(Radius.circular(6.0)),
                ),
                child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    child: Material(
                      color: Colors.transparent,
                      child: Ink(
                        child: InkWell(
                          borderRadius: BorderRadius.all(Radius.circular(6.0)),
                          child: Container(
                            child: Text(
                              "${WXLang.$t("authentication_restore_wallet", package: ModuleConfig.moduleName)}",
                              style:
                                  TextStyle(color: Color(0xFFECECF3), fontSize: 16),

                            ),
                            alignment: Alignment.center,
                          ),
                          onTap: () {
                            WXNavigator.pushNamed(
                                context: context,
                                routeName: '/walletCreate',
                                // arguments: WalletSource.recover
                                arguments: 1
                                // {
                                //   "isUpDate": false,
                                //   "walletSource": WalletSource.recover
                                // }
                                );
                          },
                        ),
                      ),
                    )))
          ],
        ),
      ),
    );
  }

  @override
  void didAppear() {
    super.didAppear();
    Future.delayed(Duration(milliseconds: 50),(){
      setState(() {

      });
    });
  }
}
